package cscie160.htest.bank;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public class BankServerImpl extends UnicastRemoteObject implements BankServer {

    //private byte fieldByte = 0;
    private cscie160.htest.bank.Account account;

    public static void main(String[] args){
        System.setSecurityManager(new RMISecurityManager());

        BankServerImpl server = null;
        //BankServer server = null;
        try{
            int port = 1100;

            if (args.length > 0 && !(args[0].equals("?") || args[0].equalsIgnoreCase("help"))){
                try{
                    port = Integer.parseInt(args[0]);
                } catch (NumberFormatException nfex){
                    System.out.println("Provided argument\"" + args[0] + "\" can't be a port");
                    return;
                }
            }
            System.out.println("Will try to createRegistry(" + Integer.toString(port) +")");
            java.rmi.registry.Registry registry =
 
                java.rmi.registry.LocateRegistry.createRegistry(port);

            server = new BankServerImpl();

            System.out.println("Server created. hashCode=" + server.hashCode());
            
            String serverRebindUrl = "//localhost:" + Integer.toString(port) + "/bankserver";
            Naming.rebind(serverRebindUrl, server);
            System.out.println("BankServer bound in registry on port "+Integer.toString(port));

        } catch (RemoteException rex) {
            System.out.println(server.getClass().getName() + " has thrown "+
              "RemoteException: "+ rex.getMessage());
            return;
        } catch (java.net.MalformedURLException muex){
            System.out.println(server.getClass().getName() + " has thrown " +
              "java.net.MalformedURLException: "+ muex.getMessage());
            return;
        }

        byte byteVal = 1;
        if (args.length > 1){
            try{
                byteVal = Byte.parseByte(args[1]);
            } catch (NumberFormatException nfex){
                byteVal = -1;
            }
        }

        try{
            server.setByteField(byteVal);
            System.out.println("After " + server.getClass().getName() + ".setByteField(" +
                Byte.toString(byteVal) + ") " + server.getClass().getName() + ".getByteField()="+
                server.getByteField() + ". Action performed via " + server.account.getClass().getName());
        } catch (RemoteException rex) {
            System.out.println("main of " + server.getClass().getName() + " has thrown exception "+
                rex.getClass().getName() + "; message=" + rex.getMessage());
        }
    }

    public BankServerImpl() throws RemoteException {
        super();
        account = new cscie160.htest.bank.Account();
        //makeAccount();
        account.setByteField((byte)0);
    }

    public byte getByteField ()  throws RemoteException {
        return account.getByteField();
    }
    public void setByteField (byte b) throws RemoteException {
        account.setByteField(b);
    }

    private void makeAccount() {
        cscie160.htest.bank.Account account = new cscie160.htest.bank.Account();
        this.account = account;
    }
}